{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "FINN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在处理 01 月的数据...\n",
      "01 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201301.tif\n",
      "正在处理 02 月的数据...\n",
      "02 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201302.tif\n",
      "正在处理 03 月的数据...\n",
      "03 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201303.tif\n",
      "正在处理 04 月的数据...\n",
      "04 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201304.tif\n",
      "正在处理 05 月的数据...\n",
      "05 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201305.tif\n",
      "正在处理 06 月的数据...\n",
      "06 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201306.tif\n",
      "正在处理 07 月的数据...\n",
      "07 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201307.tif\n",
      "正在处理 08 月的数据...\n",
      "08 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201308.tif\n",
      "正在处理 09 月的数据...\n",
      "09 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201309.tif\n",
      "正在处理 10 月的数据...\n",
      "10 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201310.tif\n",
      "正在处理 11 月的数据...\n",
      "11 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201311.tif\n",
      "正在处理 12 月的数据...\n",
      "12 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\\FINNviirs_201312.tif\n"
     ]
    }
   ],
   "source": [
    "#FINN\n",
    "from osgeo import gdal\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "def calculate_monthly_average(input_parent_folder, output_parent_folder):\n",
    "    # 遍历1到12月的子文件夹\n",
    "    for month in range(1, 13):\n",
    "        # 格式化子文件夹名称，例如 \"01\", \"02\", ..., \"12\"\n",
    "        month_folder = f\"{month:02d}\"\n",
    "\n",
    "        input_folder = os.path.join(input_parent_folder, month_folder)\n",
    "        #构造输出文件名，格式：FINN_YYYYMM.tif\n",
    "        output_file = os.path.join(output_parent_folder, f\"FINNviirs_{os.path.basename(input_parent_folder)}{month_folder}.tif\")\n",
    "\n",
    "        # 如果月份文件夹不存在，跳过\n",
    "        if not os.path.exists(input_folder):\n",
    "            print(f\"文件夹 {input_folder} 不存在，跳过...\")\n",
    "            continue\n",
    "\n",
    "        print(f\"正在处理 {month_folder} 月的数据...\")\n",
    "\n",
    "        arr_mean = None\n",
    "        count = 0\n",
    "        geotransform = None\n",
    "        projection = None\n",
    "\n",
    "        # 遍历该月份文件夹中的所有 TIFF 文件\n",
    "        for file in os.listdir(input_folder):\n",
    "            if file.endswith(\".tif\"):\n",
    "                tiff_path = os.path.join(input_folder, file)\n",
    "                dataset = gdal.Open(tiff_path)\n",
    "                band = dataset.GetRasterBand(1)\n",
    "                arr = band.ReadAsArray()\n",
    "\n",
    "                # 初始化累加数组\n",
    "                if arr_mean is None:\n",
    "                    arr_mean = np.zeros_like(arr, dtype=float)\n",
    "\n",
    "                # 累加每个栅格\n",
    "                arr_mean += arr\n",
    "                count += 1\n",
    "\n",
    "                # 记录地理信息\n",
    "                if geotransform is None:\n",
    "                    geotransform = dataset.GetGeoTransform()\n",
    "                if projection is None:\n",
    "                    projection = dataset.GetProjection()\n",
    "\n",
    "        # 如果没有文件，跳过该月份\n",
    "        if count == 0:\n",
    "            print(f\"{month_folder} 月没有有效的 TIFF 文件，跳过...\")\n",
    "            continue\n",
    "\n",
    "        #将单位从molecules/cm^2/s转为t/m^2/mo\n",
    "        arr_mean=arr_mean/(6.02214076*pow(10,23))*44.0095*864\n",
    "\n",
    "\n",
    "        # 保存平均(或总和)栅格为 TIFF 文件\n",
    "        nrows, ncols = arr_mean.shape\n",
    "        driver = gdal.GetDriverByName(\"GTiff\")\n",
    "        output_dataset = driver.Create(output_file, ncols, nrows, 1, gdal.GDT_Float32)\n",
    "        output_dataset.SetGeoTransform(geotransform)\n",
    "        output_dataset.SetProjection(projection)\n",
    "\n",
    "        output_band = output_dataset.GetRasterBand(1)\n",
    "        output_band.WriteArray(arr_mean)\n",
    "        output_band.FlushCache()\n",
    "        output_band = None\n",
    "\n",
    "        print(f\"{month_folder} 月平均 TIFF 文件已保存: {output_file}\")\n",
    "\n",
    "# 主函数\n",
    "def main():\n",
    "    input_parent_folder = r\"D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\"\n",
    "    output_parent_folder = r\"D:\\XiaLei_data\\output_data\\tif\\FINN\\VIIRS\\2013\\monthly_postprocess\"\n",
    "\n",
    "    # 如果输出父文件夹不存在，则创建\n",
    "    if not os.path.exists(output_parent_folder):\n",
    "        os.makedirs(output_parent_folder)\n",
    "\n",
    "    # 调用计算函数\n",
    "    calculate_monthly_average(input_parent_folder, output_parent_folder)\n",
    "\n",
    "main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GFAS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在处理 01 月的数据...\n",
      "01 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201301.tif\n",
      "正在处理 02 月的数据...\n",
      "02 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201302.tif\n",
      "正在处理 03 月的数据...\n",
      "03 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201303.tif\n",
      "正在处理 04 月的数据...\n",
      "04 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201304.tif\n",
      "正在处理 05 月的数据...\n",
      "05 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201305.tif\n",
      "正在处理 06 月的数据...\n",
      "06 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201306.tif\n",
      "正在处理 07 月的数据...\n",
      "07 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201307.tif\n",
      "正在处理 08 月的数据...\n",
      "08 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201308.tif\n",
      "正在处理 09 月的数据...\n",
      "09 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201309.tif\n",
      "正在处理 10 月的数据...\n",
      "10 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201310.tif\n",
      "正在处理 11 月的数据...\n",
      "11 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201311.tif\n",
      "正在处理 12 月的数据...\n",
      "12 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\\GFAS_201312.tif\n"
     ]
    }
   ],
   "source": [
    "#GFAS\n",
    "from osgeo import gdal\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "def calculate_monthly_average(input_parent_folder, output_parent_folder):\n",
    "    # 遍历1到12月的子文件夹\n",
    "    for month in range(1, 13):\n",
    "        # 格式化子文件夹名称，例如 \"01\", \"02\", ..., \"12\"\n",
    "        month_folder = f\"{month:02d}\"\n",
    "        input_folder = os.path.join(input_parent_folder, month_folder)\n",
    "        \n",
    "        output_file = os.path.join(output_parent_folder, f\"GFAS_{os.path.basename(input_parent_folder)}{month_folder}.tif\")\n",
    "\n",
    "        # 如果月份文件夹不存在，跳过\n",
    "        if not os.path.exists(input_folder):\n",
    "            print(f\"文件夹 {input_folder} 不存在，跳过...\")\n",
    "            continue\n",
    "\n",
    "        print(f\"正在处理 {month_folder} 月的数据...\")\n",
    "\n",
    "        arr_mean = None\n",
    "        count = 0\n",
    "        geotransform = None\n",
    "        projection = None\n",
    "\n",
    "        # 遍历该月份文件夹中的所有 TIFF 文件\n",
    "        for file in os.listdir(input_folder):\n",
    "            if file.endswith(\".tif\"):\n",
    "                tiff_path = os.path.join(input_folder, file)\n",
    "                dataset = gdal.Open(tiff_path)\n",
    "                band = dataset.GetRasterBand(1)\n",
    "                arr = band.ReadAsArray()\n",
    "\n",
    "                # 初始化累加数组\n",
    "                if arr_mean is None:\n",
    "                    arr_mean = np.zeros_like(arr, dtype=float)\n",
    "\n",
    "                # 累加每个栅格\n",
    "                arr_mean += arr\n",
    "                count += 1\n",
    "\n",
    "                # 记录地理信息\n",
    "                if geotransform is None:\n",
    "                    geotransform = dataset.GetGeoTransform()\n",
    "                if projection is None:\n",
    "                    projection = dataset.GetProjection()\n",
    "\n",
    "        # 如果没有文件，跳过该月份\n",
    "        if count == 0:\n",
    "            print(f\"{month_folder} 月没有有效的 TIFF 文件，跳过...\")\n",
    "            continue\n",
    "\n",
    "        #将单位从kg/m^2/s转为t/m^2/mo\n",
    "        arr_mean=arr_mean*86.4\n",
    "\n",
    "        # 保存平均(或总和)栅格为 TIFF 文件\n",
    "        nrows, ncols = arr_mean.shape\n",
    "        driver = gdal.GetDriverByName(\"GTiff\")\n",
    "        output_dataset = driver.Create(output_file, ncols, nrows, 1, gdal.GDT_Float32)\n",
    "        output_dataset.SetGeoTransform(geotransform)\n",
    "        output_dataset.SetProjection(projection)\n",
    "\n",
    "        output_band = output_dataset.GetRasterBand(1)\n",
    "        output_band.WriteArray(arr_mean)\n",
    "        output_band.FlushCache()\n",
    "        output_band = None\n",
    "\n",
    "        print(f\"{month_folder} 月平均 TIFF 文件已保存: {output_file}\")\n",
    "\n",
    "# 主函数\n",
    "def main():\n",
    "    input_parent_folder = r\"D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\"\n",
    "    output_parent_folder = r\"D:\\XiaLei_data\\output_data\\tif\\GFAS\\2013\\monthly_postprocess\"\n",
    "\n",
    "    # 如果输出父文件夹不存在，则创建\n",
    "    if not os.path.exists(output_parent_folder):\n",
    "        os.makedirs(output_parent_folder)\n",
    "\n",
    "    # 调用计算函数\n",
    "    calculate_monthly_average(input_parent_folder, output_parent_folder)\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GFED"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在处理 01 月的数据...\n",
      "01 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201301.tif\n",
      "正在处理 02 月的数据...\n",
      "02 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201302.tif\n",
      "正在处理 03 月的数据...\n",
      "03 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201303.tif\n",
      "正在处理 04 月的数据...\n",
      "04 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201304.tif\n",
      "正在处理 05 月的数据...\n",
      "05 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201305.tif\n",
      "正在处理 06 月的数据...\n",
      "06 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201306.tif\n",
      "正在处理 07 月的数据...\n",
      "07 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201307.tif\n",
      "正在处理 08 月的数据...\n",
      "08 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201308.tif\n",
      "正在处理 09 月的数据...\n",
      "09 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201309.tif\n",
      "正在处理 10 月的数据...\n",
      "10 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201310.tif\n",
      "正在处理 11 月的数据...\n",
      "11 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201311.tif\n",
      "正在处理 12 月的数据...\n",
      "12 月平均 TIFF 文件已保存: D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\\GFED_201312.tif\n"
     ]
    }
   ],
   "source": [
    "#GFED\n",
    "from osgeo import gdal\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "def calculate_monthly_average(input_parent_folder, output_parent_folder):\n",
    "    # 遍历1到12月的子文件夹\n",
    "    for month in range(1, 13):\n",
    "        # 格式化子文件夹名称，例如 \"01\", \"02\", ..., \"12\"\n",
    "        month_folder = f\"{month:02d}\"\n",
    "        input_folder = os.path.join(input_parent_folder, month_folder)\n",
    "        #注意根据待处理数据集的名称更改此输出文件名\n",
    "        output_file = os.path.join(output_parent_folder, f\"GFED_{os.path.basename(input_parent_folder)}{month_folder}.tif\")\n",
    "\n",
    "        # 如果月份文件夹不存在，跳过\n",
    "        if not os.path.exists(input_folder):\n",
    "            print(f\"文件夹 {input_folder} 不存在，跳过...\")\n",
    "            continue\n",
    "\n",
    "        print(f\"正在处理 {month_folder} 月的数据...\")\n",
    "\n",
    "        arr_mean = None\n",
    "        count = 0\n",
    "        geotransform = None\n",
    "        projection = None\n",
    "\n",
    "        # 遍历该月份文件夹中的所有 TIFF 文件\n",
    "        for file in os.listdir(input_folder):\n",
    "            if file.endswith(\".tif\"):\n",
    "                tiff_path = os.path.join(input_folder, file)\n",
    "                dataset = gdal.Open(tiff_path)\n",
    "                band = dataset.GetRasterBand(1)\n",
    "                arr = band.ReadAsArray()\n",
    "\n",
    "                # 初始化累加数组\n",
    "                if arr_mean is None:\n",
    "                    arr_mean = np.zeros_like(arr, dtype=float)\n",
    "\n",
    "                # 累加每个栅格\n",
    "                arr_mean += arr\n",
    "                count += 1\n",
    "\n",
    "                # 记录地理信息\n",
    "                if geotransform is None:\n",
    "                    geotransform = dataset.GetGeoTransform()\n",
    "                if projection is None:\n",
    "                    projection = dataset.GetProjection()\n",
    "\n",
    "        # 如果没有文件，跳过该月份\n",
    "        if count == 0:\n",
    "            print(f\"{month_folder} 月没有有效的 TIFF 文件，跳过...\")\n",
    "            continue\n",
    "\n",
    "        #将单位从g/m^2/month转为t/m^2/mo\n",
    "        arr_mean=arr_mean*pow(10,-6)\n",
    "\n",
    "        # 保存平均(或总和)栅格为 TIFF 文件\n",
    "        nrows, ncols = arr_mean.shape\n",
    "        driver = gdal.GetDriverByName(\"GTiff\")\n",
    "        output_dataset = driver.Create(output_file, ncols, nrows, 1, gdal.GDT_Float32)\n",
    "        output_dataset.SetGeoTransform(geotransform)\n",
    "        output_dataset.SetProjection(projection)\n",
    "\n",
    "        output_band = output_dataset.GetRasterBand(1)\n",
    "        output_band.WriteArray(arr_mean)\n",
    "        output_band.FlushCache()\n",
    "        output_band = None\n",
    "\n",
    "        print(f\"{month_folder} 月平均 TIFF 文件已保存: {output_file}\")\n",
    "\n",
    "# 主函数\n",
    "def main():\n",
    "    input_parent_folder = r\"D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\"\n",
    "    output_parent_folder = r\"D:\\XiaLei_data\\output_data\\tif\\GFED\\2013\\monthly_postprocess\"\n",
    "\n",
    "    # 如果输出父文件夹不存在，则创建\n",
    "    if not os.path.exists(output_parent_folder):\n",
    "        os.makedirs(output_parent_folder)\n",
    "\n",
    "    # 调用计算函数\n",
    "    calculate_monthly_average(input_parent_folder, output_parent_folder)\n",
    "\n",
    "main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
